﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class controls_urlLanguage : System.Web.UI.UserControl
{
    string selectedLanguage;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Language"] != null)
            {
                if (Session["Language"].ToString() == "th-TH")
                {
                    //lbtTHLanguage.PostBackUrl = "";
                    //lbtTHLanguage.Enabled = false;
                    //lbtTHLanguage.Font.Underline = false;
                    lbtTHLanguage.Visible = false;
                    lbtENLanguage.Visible = true;
                    imgLanguage.ImageUrl = "/images/icon/lang_en.png";
                    imgLanguage.Attributes.Add("width", "19");
                    imgLanguage.Attributes.Add("height", "19");

                    lbtENLanguage.OnClientClick = "javascript:return confirm('" + Resources.CarInsurance.LanguageChangeMessage + "');";
                }
                else if (Session["Language"].ToString() == "en-US")
                {
                    //lbtENLanguage.PostBackUrl = "";
                    //lbtENLanguage.Enabled = false;
                    //lbtENLanguage.Font.Underline = false;
                    lbtENLanguage.Visible = false;
                    lbtTHLanguage.Visible = true;
                    imgLanguage.ImageUrl = "/images/icon/lang_th.png";
                    imgLanguage.Attributes.Add("width", "19");
                    imgLanguage.Attributes.Add("height", "19");

                    lbtTHLanguage.OnClientClick = "javascript:return confirm('" + Resources.CarInsurance.LanguageChangeMessage + "');";
                }
            }
        }
    }


    protected void btnLanguage_Click(object sender, EventArgs e)
    {
        selectedLanguage = string.Empty;

        if (lblLanguage.Text != string.Empty)
        {
            Session["Language"] = lblLanguage.Text;
        }

        if (Session["Language"] != null)
        {
            selectedLanguage = Session["Language"].ToString();
        }
        else
        {
            selectedLanguage = "th-TH";
            Session["Language"] = selectedLanguage;
        }

        if (selectedLanguage == "th-TH")
        {
            Session["Language"] = "en-US";
        }
        else
        {
            Session["Language"] = "th-TH";
        }

        lblLanguage.Text = selectedLanguage;

        if (selectedLanguage == "en-US")
        {
            Response.Redirect(Request.UrlReferrer.AbsoluteUri.Replace("/en", ""));
        }
        else
        {
            string[] _arrList = Request.UrlReferrer.AbsoluteUri.Split('/');
            string _redirectUrl = "";
            _arrList[3] = "en/" + _arrList[3];
            for (int i = 0; i < _arrList.Length; i++)
            {

                if (i != _arrList.Length - 1)
                {
                    _redirectUrl += _arrList[i].ToString() + "/";
                }
                else
                {
                    _redirectUrl += _arrList[i].ToString();
                }

            }
            if (_arrList.Length == 4 && _redirectUrl.EndsWith("/"))
            {
                _redirectUrl += "home.aspx";
            }

            Response.Redirect(_redirectUrl);
        }
    }
}
